package com.ruoyi.scale.controller;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.scale.assembler.ScaleAssembler;
import com.ruoyi.scale.document.Scale;
import com.ruoyi.scale.service.ScaleService;
import org.springframework.data.domain.Example;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/scale")
public class ScaleController extends BaseController {
    @Resource
    private ScaleAssembler scaleAssembler;
    @Resource
    private ScaleService scaleService;
    /**
     * 获取量表基础信息
     */
    @Anonymous
    //@PreAuthorize("@ss.hasPermi('*:*:*')")
    @GetMapping(value = "/getById")
    public AjaxResult getInfo(@RequestParam("id") String id) {
        return success(scaleAssembler.toVo(scaleService.getById(id).orElse(null)));
    }
    @Anonymous
    @GetMapping(value = "/getScaleByCode")
    public AjaxResult getScaleByCode(@RequestParam("code") String code){
        Scale scale =new Scale();
        scale.setCode(code);
        Example<Scale> of = Example.of(scale);
        List<Scale> all = scaleService.findAll(of);
        if(all.size() != 1){
            return error("无效编码!");
        }
        return success(all.get(0));
    }
}
